Skip to content

Conversation

@rniczh
Copy link

@rniczh rniczh commented Jan 6, 2026

Context:

from catalyst import *
import pennylane as qml

qml.capture.enable()

@qjit
@qml.qnode(qml.device("lightning.qubit", wires=3, shots=10))
def circuit():
    with qml.allocate(2) as qs:
        qml.X(qs[1])

    return qml.sample(wires=[0, 1])

print(circuit())

It get unexpected result due to the aux wires involved.

[[0 0]
 [0 1]
 [0 0]
 [1 0]
 [0 0]
 [0 0]
 [0 1]
 [0 0]
 [1 0]
 [0 0]]

Description of the Change:

Only take the device wires for generating samples
Compact state vector before measurement.

For state vector normalization:

$|\psi| = \sqrt{\sum_i |\psi_i|^2}$
$normalized_i = \psi_i / |\psi|$

Benefits:

Possible Drawbacks:

Related GitHub Issues:

PennyLaneAI/catalyst#2339
[[sc-107206]]

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@blacksmith-sh

This comment has been minimized.

Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rniczh !

const std::vector<std::size_t> &wires, const std::string &kernelname,
std::size_t num_burnin, std::size_t num_samples) {
std::uniform_real_distribution<PrecisionT> distrib(0.0, 1.0);
std::size_t num_qubits = wires.size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know that this is sufficient to identify which wires should be selected in a more complex dynamic (de)allocation scheme, or does it rely on the assumption that the selected wires are always the bottom n wires?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned this offline but I think we'd be better off providing a universal solution that we never got around to implementing (see #1254 (comment)).

Copy link
Author

@rniczh rniczh Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR, basically removed the original solution that only pass the device_wires to GenerateSample. Now, it will compact the state vector before the measurement process to ensure the erased qubits are erased from state vector as well.
ca0ff43 Edit: 77fbbe9

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow that was fast! 🤩
@maliasadi are you able to look this over?

@blacksmith-sh

This comment has been minimized.

@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

❌ Patch coverage is 99.58333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.89%. Comparing base (e7d82a2) to head (6c8cdb2).

Files with missing lines Patch % Lines
...rs/lightning_qubit/catalyst/LightningSimulator.cpp 98.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           v0.44.0_rc    #1321      +/-   ##
==============================================
- Coverage       96.01%   93.89%   -2.13%     
==============================================
  Files             307      243      -64     
  Lines           46923    40264    -6659     
==============================================
- Hits            45055    37804    -7251     
- Misses           1868     2460     +592     
Flag Coverage Δ
unit_tests 93.89% <99.58%> (-2.13%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@blacksmith-sh

This comment has been minimized.

@maliasadi maliasadi self-requested a review January 8, 2026 15:06
Copy link
Member

@maliasadi maliasadi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rniczh! A few things:

  • If we need this to go out with the release, you will need to rebase it against #1317 and sync ASAP with @jzaia18
  • Update Lighnting-Kokkos and Lightning-GPU catalyst impl similar to Lightning-Qubit
  • Please add a changelog entry for this fix

@rniczh rniczh changed the base branch from master to v0.44.0_rc January 8, 2026 15:19
@jzaia18 jzaia18 added this to the 0.44.0 milestone Jan 8, 2026
@rniczh rniczh force-pushed the rniczh/fix-dynamic-wires-with-samples branch from d3abf70 to 6e9f177 Compare January 8, 2026 15:32
@rniczh rniczh changed the base branch from v0.44.0_rc to master January 8, 2026 15:37
@rniczh rniczh changed the base branch from master to v0.44.0_rc January 8, 2026 15:37
Copy link
Contributor

@jzaia18 jzaia18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work Hongsheng! Just a few questions and comments. Is there any reason we're implementing this for lightning.qubit but not the other devices? Does this issue exist for the others as well? nvm, just saw Ali already addressed this

@rniczh rniczh removed the ci:use-gpu-runner Enable usage of GPU runner for this Pull Request label Jan 8, 2026
*(this->device_sv)};
}

void LightningGPUSimulator::CompactStateVector() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these tested? I couldn't find any tests for these methods for all devices.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is triggered by getMeasurements(), ensuring that any test executing the measurement process also updates the state vector. Currently, only one test case is added (the one that reported from the issue PennyLaneAI/catalyst#2339). The specific test can be found here:

https://github.com/PennyLaneAI/pennylane-lightning/pull/1321/changes#diff-6f0e99f87b0a9753689936392636b52eb19b81aac144b24d12de297f3c617447R335

Copy link
Member

@maliasadi maliasadi Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be added to Catalyst for the release?

per our offline discussions, what if these aux wires are entangled, we should test and add some handling mechanism for these cases in both Lightning & Catalyst?

auto GenerateSamples(size_t shots) -> std::vector<size_t>;

// Compact state vector by removing released qubits
void CompactStateVector();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: this is the style we've been following for a while:

Suggested change
void CompactStateVector();
void compactStateVector();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call this reducedStateVector instead of compact?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I updated the name here: a5e2132

@jzaia18
Copy link
Contributor

jzaia18 commented Jan 8, 2026

Could you also bump the rc version to 3 within pennylane_lightning/core/_version.py?

@rniczh
Copy link
Author

rniczh commented Jan 9, 2026

Could you also bump the rc version to 3 within pennylane_lightning/core/_version.py?

Sure abd373d

@rniczh rniczh added the ci:use-gpu-runner Enable usage of GPU runner for this Pull Request label Jan 9, 2026
Copy link
Contributor

@jzaia18 jzaia18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just 1 last comment

@paul0403 paul0403 requested a review from mlxd January 9, 2026 18:43
@jzaia18 jzaia18 removed this from the 0.44.0 milestone Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:use-gpu-runner Enable usage of GPU runner for this Pull Request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants